<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/*
 * 微信支付回调
*/
class Wxpaynotify extends BaseController {

	public function __construct() {
        parent::__construct();

        // 加载类库
        $this->load->model('schoolModel');
        $this->load->model('classesModel');
        $this->load->model('contentModel');
        $this->load->model('dataModel');
        $this->load->model('wxUserModel');
    }
    
    // 卡号续费
    public function card() {
		//接收
        $xml = file_get_contents("php://input");
        $object = $this->xml_to_object($xml);

        if($object->result_code == 'FAIL') {
            $return['result_code'] = 'SUCCESS';
            exit($this->array_to_xml($return));
        }
        //返回值
        $return['return_code'] = 'FAIL';
        $return['return_msg'] = '';
        //签名验证
        foreach($object as $k => $v) {
            if($k != 'sign') $tdata[$k] = $k. "=". $v;
        }
        ksort($tdata);
        $stringA = implode("&", $tdata). "&key=". WX_PAY_KEY;
        $sign = strtoupper(md5($stringA));
        if($sign != $object->sign) {
            $return['return_msg'] = '签名失败';
            exit($this->array_to_xml($return));
        }
        // 处理代码
        $outTradeNo = $object->out_trade_no;
        // $this->db->update(SchoolModel::$schoolUserTable, array(
        //     'content' => $outTradeNo
        // ), array('id' => 1372));
        // 获取支付信息
        $chargePay = $this->wxUserModel->get_charge_pay_info_by_trade_no($outTradeNo);
        // 更新支付信息
        $this->db->update(WxUserModel::$chargePayTable, array(
            'is_paid' => 1,
            'paytime' => date('Y-m-d H:i:s')
        ), array(
            'out_trade_no' => $outTradeNo
        ));
        // 更新学生过期时间
        $this->db->update(SchoolModel::$schoolStudentTable, array(
            'expire_date' => $chargePay['expire_date'],
            'updatetime' => date('Y-m-d H:i:s')
        ), array(
            'id' => $chargePay['student_id']
        ));

        $studentInfo = $this->schoolModel->get_student_info($chargePay['student_id']);
        // 学生有人脸或卡号才需处理
        if($studentInfo['face_image'] || $studentInfo['realcard']) {
            // 插入闸机白名单任务
            // $gateParams['schoolId'] = $studentInfo['school_id'];
            // $gateParams['campusId'] = $studentInfo['campus_id'];
            // // 过滤软删除
            // $gateParams['isDelete'] = 0;
            // $gateList = $this->schoolModel->get_gate_list($gateParams);
            // if($gateList) {

            //     $insertTaskBatch = array();
            //     foreach($gateList as $gate) {
            //         $arr = $this->schoolModel->format_gate_task($studentInfo['school_id'], $studentInfo['campus_id'], $gate['id'], $gate['device_sn'], $studentInfo['id'],
            //             $studentInfo['realname'], $studentInfo['face_image'], GATE_USER_TYPE_STUDENT, GATE_TASK_TYPE_WHITE_LIST, GATE_TASK_STATE_NEW, '');
            //         array_push($insertTaskBatch, $arr);
            //     }
            //     if($insertTaskBatch) {
            //         // 插入闸机任务
            //         $this->schoolModel->insert_gate_task_batch($insertTaskBatch);
            //     }
            // }
            // $this->load->model('gateModel');
            // $this->gateModel->add_gate_task($studentInfo['school_id'], $studentInfo['campus_id'], GATE_TASK_TYPE_WHITE_LIST, $studentInfo, GATE_USER_TYPE_STUDENT);
        }
        
        $return['return_code'] = 'SUCCESS';
        exit($this->array_to_xml($return));
    }

    //xml转对象
    private function xml_to_object($xml){
        $ob = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        return $ob;
    }

	private function array_to_xml($arr) {
		if(!is_array($arr) || count($arr) <= 0)
		{
    		throw new Exception("数组数据异常！");
    	}
    	
    	$xml = "<xml>";
    	foreach ($arr as $key=>$val)
    	{
    		if (is_numeric($val)){
    			$xml.="<".$key.">".$val."</".$key.">";
    		}else{
    			$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
    		}
        }
        $xml.="</xml>";
        return $xml; 
	}    


}